package studykana.utils;

import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

public class SwapViews implements Runnable {

	private boolean mIsFirstView;
	ImageView imageFrontCard;
	TextView tvBackCard;

	public SwapViews(boolean isFirstView, ImageView imageFrontCard, TextView tvBackCard) {
		mIsFirstView = isFirstView;
		this.imageFrontCard = imageFrontCard;
		this.tvBackCard = tvBackCard;
	}

	public void run() {
		final float centerX = imageFrontCard.getWidth() / 2.0f;
		final float centerY = imageFrontCard.getHeight() / 2.0f;
		Flip3dAnimation rotation;

		if (mIsFirstView) {
			imageFrontCard.setVisibility(View.GONE);
			tvBackCard.setVisibility(View.VISIBLE);
			tvBackCard.requestFocus();

			rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
		} else {
			tvBackCard.setVisibility(View.GONE);
			imageFrontCard.setVisibility(View.VISIBLE);
			imageFrontCard.requestFocus();

			rotation = new Flip3dAnimation(90, 0, centerX, centerY);
		}

		rotation.setDuration(500);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new DecelerateInterpolator());

		if (mIsFirstView) {
			tvBackCard.startAnimation(rotation);
		} else {
			imageFrontCard.startAnimation(rotation);
		}
	}
}
